package edu.fivegroup.mall.goods;



import edu.fivegroup.mall.util.ConnectionDatabaseForMySQLUseJdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class GoodsDaoImpl implements GoodsDao {
    Connection connection = null;
    PreparedStatement pstmt = null;
    ResultSet rset = null;

    public GoodsDaoImpl(){connection = ConnectionDatabaseForMySQLUseJdbc.getConnection();}
    @Override
    public int addGoods(Goods goods) {
        try{
            pstmt = connection.prepareStatement("insert into t_goods(title,price,descr,img,state,mid) values(?,?,?,?,?,?)");
            pstmt.setString(1, goods.getTitle());
            pstmt.setBigDecimal(2, goods.getPrice());
            pstmt.setString(3, goods.getDescr());
            pstmt.setString(4, goods.getImg());
            pstmt.setInt(5, 0);
            pstmt.setInt(6, goods.getMid());
            int i = pstmt.executeUpdate();
            return i == 1 ? 1 : 0;      //三目运算符
        } catch (Exception exception) {
            exception.printStackTrace();

        }
        return 0;
    }

    @Override
    public int updateGoods(Goods goods) {
        try{
            pstmt = connection
                    .prepareStatement("UPDATE t_goods SET title =?, price =?, descr =?, img =?, mid =? WHERE id = ?");
            pstmt.setString(1, goods.getTitle()); //设置参数
            pstmt.setBigDecimal(2, goods.getPrice());
            pstmt.setString(3, goods.getDescr());
            pstmt.setString(4, goods.getImg());
            pstmt.setInt(5, goods.getMid());
            pstmt.setInt(6, goods.getId());

            int i = pstmt.executeUpdate();
            return i == 1 ? 1 : 0;
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;

    }

    @Override
    public List<Goods> findAllGoods(int page, int size,String keyword) {
        List<Goods> list = new ArrayList<>();
        try{
            String sql = "SELECT id, title, price, descr, img, state, mid FROM t_goods WHERE title LIKE ? OR descr LIKE ? LIMIT ?, ?";
            pstmt = connection.prepareStatement(sql);
            pstmt.setString(1, "%" + keyword + "%");
            pstmt.setString(2, "%" + keyword + "%");
            pstmt.setInt(3, (page - 1) * size);
            pstmt.setInt(4, size);

            rset = pstmt.executeQuery();
            while (rset.next()) {
                Goods goods = new Goods(
                        rset.getInt("id"),
                        rset.getString("title"),
                        rset.getBigDecimal("price"),
                        rset.getString("descr"),
                        rset.getString("img"),
                        rset.getInt("state"),
                        rset.getInt("mid")
                );
                list.add(goods);
            }
            return list;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int getTotalGoods(String keyword) {
        try {
            String sql = "SELECT COUNT(*) AS total FROM t_goods WHERE title LIKE ? OR descr LIKE ?";
            pstmt = connection.prepareStatement(sql);
            pstmt.setString(1, "%" + keyword + "%");
            pstmt.setString(2, "%" + keyword + "%");
            rset = pstmt.executeQuery();
            if (rset.next()) {
                return rset.getInt("total");
            }
            return 0;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Goods findGoodsById(int id) {
        try{
            String sql = "SELECT id, title, price, descr, img, state, mid FROM t_goods WHERE id = ?";
            pstmt = connection.prepareStatement(sql);
            pstmt.setInt(1, id);
            rset = pstmt.executeQuery();
            if (rset.next()) {
                Goods goods = new Goods(
                        rset.getInt("id"),
                        rset.getString("title"),
                        rset.getBigDecimal("price"),
                        rset.getString("descr"),
                        rset.getString("img"),
                        rset.getInt("state"),
                        rset.getInt("mid")
                );
                return goods;
            }
        }catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return null;

    }

    @Override
    public int deleteGoods(int id) {
        try{
            pstmt = connection.prepareStatement("UPDATE t_goods SET state = 1 WHERE id = ?");
            pstmt.setInt(1, id);
            return pstmt.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
